package com.yenlien.traceyun.usercenter.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.usercenter.bean.AppMenuTree;
import com.yenlien.traceyun.usercenter.entity.Department;
import com.yenlien.traceyun.usercenter.entity.Resource;
import com.yenlien.traceyun.usercenter.service.DepartmentService;
import com.yenlien.traceyun.usercenter.service.ResourceService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by LiuSha on 2020/6/12.
 */
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ResourceService resourceService;

    @GetMapping(value = "/company/chain/{companyId}")
    public Object departmentChain(@PathVariable("companyId") String id) {
        return success(departmentService.getChain(id));
    }

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Department department) {
        return buildPage(departmentService.findByList(department));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Department department) {
        departmentService.save(department);
        return success("success");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody Department department) {
        departmentService.update(department);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(departmentService.findById(id));
    }

    @RequestMapping(value = "/findByAll/{companyId}", method = RequestMethod.GET)
    public Object findByAll(@PathVariable String companyId) {
        return success(departmentService.findByAll(companyId));
    }


    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable("id") String id) {
        departmentService.dele(id);
        return success("success");
    }


    //查看部门权限
    @RequestMapping(value = "/findByMenu/{id}", method = RequestMethod.GET)
    public Object findByMenu(@PathVariable String id) throws BusinessException {
        return success(resourceService.findByMenu(id));
    }

    //添加部门权限
    @RequestMapping(value = "/saveMenu", method = RequestMethod.POST)
    public Object saveMenu(@RequestBody Department department) throws BusinessException {
        resourceService.saveDeptMenu(department);
        return success("success");
    }

    /******修改*****/


    /**
     * 绑定资源
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/bindresource", method = RequestMethod.POST)
    public Object bindResource(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"departmentId", "menus"});
        JSONArray resources = params.getJSONArray("menus");
        departmentService.bindResource(params.getString("departmentId"), resources.toJavaList(Resource.class));
        return success("success");
    }

    /**
     * 加载企业已有资源
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/loadresource/{id}")
    public Object loadResource(@PathVariable("id") String id) {
        List<AppMenuTree> result = resourceService.loadDepartemntMenus(id);
        return success(result);
    }

}
